/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import net.sourceforge.perforce.api.internal.ClientApi;
import org.eclipse.core.boot.BootLoader;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;

/**
  * @version $Revision: 39 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net>Boris Pruessmann</a>
  */
public class PerforceApiPlugin extends Plugin 
{
	// ---------------------------------------------------------------------------- Public Constants
	
	/** The unique identifier of the plugin. */
	public static final String ID = "net.sourceforge.perforce.api";                    //$NON-NLS-1$
	
	// -------------------------------------------------------------------- Private Static Variables
	
	/** Stores the singleton instance, returned by getPlugin(). */
	private static PerforceApiPlugin plugin;
	
	// ------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for PerforceApiPlugin.
	  * @param descriptor the plugin's descriptor.
	  */
	public PerforceApiPlugin(IPluginDescriptor descriptor) 
	{
		super(descriptor);
		plugin = this;
	}

	// ----------------------------------------------------------------------- Public Static Methods
	
	/** Returns the sinlgeton instance of the plugin.
	  * 
	  * @return the singleton instance.
	  */
	public static PerforceApiPlugin getPlugin()
	{
		return plugin;
	}
	
	/** Creates a class implementing the IClientApi interface.
	  *
	  * @return the IClientApi compatible instance.
	  */
	public static IClientApi createClientApi()
	{
		return new ClientApi();
	}

	// ------------------------------------------------------------------------------ Public Methods
	
	/** @see Plugin#startup() */
	public void startup() 
		throws CoreException 
	{
		super.startup();
		Policy.localize("net.sourceforge.perforce.api.messages");
		
		// On MacOS X, we have to split the jni library into two parts. Unfortunately,
		// we have to place one library in a directory on the path, that's why have to
		// make a copy here.
		if (BootLoader.getOS().equals(BootLoader.OS_MACOSX))
		{
			try 
			{
				InputStream is = find(new Path("$os$/libperforce.dylib")).openStream();
				FileOutputStream os = new FileOutputStream("libperforce.dylib");
				
				byte[] buffer = new byte[8192];
				int read = -1;
				while ((read = is.read(buffer)) != -1)
				{
					os.write(buffer, 0, read);
				}
				os.close();
				is.close();
			} 
			catch (FileNotFoundException e) 
			{
				Status status = new Status(Status.ERROR, ID, 0, 
					Policy.bind("PerforceApiPlugin.startup"), e);                      //$NON-NLS-1$
					
				throw new CoreException(status);
			} 
			catch (IOException e) 
			{
				Status status = new Status(Status.ERROR, ID, 0, 
					Policy.bind("PerforceApiPlugin.startup"), e);                      //$NON-NLS-1$
					
				throw new CoreException(status);
			}
		}
			
		try
		{
			System.loadLibrary("perforce"); 
		}
		catch (UnsatisfiedLinkError e)
		{
			Status status = new Status(Status.ERROR, ID, 0, 
				Policy.bind("PerforceApiPlugin.startup"), e);                          //$NON-NLS-1$
				
			throw new CoreException(status);
		}
	}
	
	/** @see org.eclipse.core.runtime.Plugin#shutdown() */
	public void shutdown() 
		throws CoreException 
	{
		if (BootLoader.getOS().equals(BootLoader.OS_MACOSX))
		{
			// Remove libperforce.dylib that we have to copy on MacOS X
			File file = new File("libperforce.dylib");
			if (file.exists())
			{
				file.delete();
			}
		}
		
		super.shutdown();
	}

}
